package com.rfid.emergencywarehouse.fragment;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.fragment.NavHostFragment;

import com.rfid.base.util.AnimatorHelper;
import com.rfid.emergencywarehouse.R;
import com.rfid.emergencywarehouse.adapter.CreateListAdapter;
import com.rfid.emergencywarehouse.adapter.ScanBarcodeAdapter;
import com.rfid.emergencywarehouse.entity.BarcodeInfo;
import com.rfid.emergencywarehouse.entity.NameValueBean;
import com.rfid.emergencywarehouse.manager.FragmentDataManager;
import com.rfid.emergencywarehouse.presenter.CreateListingPresenter;
import com.rfid.emergencywarehouse.util.DialogUtil;
import com.rfid.emergencywarehouse.view.ICreateListingView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CreateListingFragment extends BaseScanListFragment<CreateListAdapter, BarcodeInfo, CreateListingPresenter> implements ICreateListingView, FragmentDataManager.Listener {

    List<NameValueBean> mNameValueBeans;
    NameValueBean mSelectKW;
    int mClickId;

    @BindView(R.id.barcode)
    EditText mBarcodeEdit;

    @BindView(R.id.kw_name)
    TextView mKWName;

    @BindView(R.id.scan)
    CheckBox mRfid;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,view);
    }

    @Override
    protected int getScanRfidCheckBoxId() {
        return R.id.scan;
    }

    @Override
    protected EditText getBarcodeEdit() {
        return mBarcodeEdit;
    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        FragmentDataManager.INSTANCE.addListener(this);
        setTitle("新建上架单");
        mPresenter.getKWAndYJG();
    }

    @Override
    public void onDetach() {
        super.onDetach();
        FragmentDataManager.INSTANCE.removeListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        showSelectInfo();
    }

    @Override
    public void onSelect(NameValueBean v) {
        mSelectKW = v;
        showSelectInfo();
    }

    private void showSelectInfo() {
        if (mSelectKW != null) {
            mKWName.setText(mSelectKW.getName());
        }
    }

//    @Override
//    protected String getScanTip(int size) {
//        return String.format("已扫描数据%s条，请选择你要上架的物资",size);
//    }

    @Override
    protected CreateListAdapter getAdapter() {
        return new CreateListAdapter(mData);
    }

    @Override
    public CreateListingPresenter getPresenter() {
        return new CreateListingPresenter();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_create_listing;
    }

    @Override
    public void onGetKWYJGList(List<NameValueBean> beans) {
        mNameValueBeans = beans;
    }

    @Override
    public void onCreateListing(boolean s, String msg) {
        if (s) {
            showSuccess("新建成功");
            clearSelectData();
        } else {
//            Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            showTip(msg);
        }
    }

    @OnClick({R.id.kw})
    public void click(View v) {
        int id = v.getId();
        System.out.println("click id = " + id);
        mClickId = id;
        FragmentDataManager.INSTANCE.select(null);
        FragmentDataManager.INSTANCE.setData(null);
        if (id == R.id.kw) {
            if (mNameValueBeans != null) {
                FragmentDataManager.INSTANCE.setData(mNameValueBeans);
            } else {
                mPresenter.getKWAndYJG();
            }
        }
        NavHostFragment.findNavController(this).navigate(R.id.action_createOrderFragment_to_select);
    }


    @OnClick(R.id.add)
    public void add(View v) {
        System.out.println("add");
        if (mSelectKW == null) {
            AnimatorHelper.shake(getContext(),mKWName);
            return;
        }
        List<String> barcodes = getBarcodes();
        if (barcodes == null) {
            return;
        }
        mPresenter.createListing(mSelectKW.getValue(),barcodes);
    }

    @Override
    protected boolean needAdd(BarcodeInfo info) {
        return !info.isNew();
    }
}